<?php
class Main extends MY_Controller {
	
	// array with the main menu
	var $menuItems;
	
	// array with the name of all views used in this controler 
	var $views=array(
		'index'=>'public/content/publicBodyMain',
		'link2'=>'public/content/publicBodyPage2',
		'link3'=>'public/content/publicBodyPage3',
		'link4'=>'public/content/publicBodyPage4',
		'login'=>'public/content/publicBodyLogin',
	);
	
	
	/**
	 * Main Constructor
	 */
	function Main() {
		parent::__construct();
		
		$this->load->model('Public_model');
		
		$this->load->helpers(array('html'));
		
		$this->load->library(array('carabiner','template'));
		
		$this->lang->load('public');
		
		$this->carabiner->css('public.css');
		
		$this->_createMenuNav();	
		
		$this->_configTemplate();
		
		$this->_setData();
	}

	
	
	/**
	 * Load and config template
	 */
	private function _configTemplate() {
		$this->template->title('Radical Booking');
		$this->template->enable_parser(false);
		$this->template->set_layout('publicLayout');
		$this->template->set_partial('footer', 		'public/partials/publicFooter');
		$this->template->set_partial('loginForm', 	'public/partials/publicLoginForm');
		$this->template->set_partial('loginInfo', 	'public/partials/publicLoginInfo');
		$this->template->set_partial('rightPanel', 	'public/partials/publicRightPanel');
	}
	
	
	
	
	
	/**
	 * Set global data to be forward to all views
	 */
	private function _setData() {
		
		$account = array();
		
		if ($this->ion_auth->logged_in()) {
			
			$account['email'] = $this->ion_auth->get_user()->email;
			
			if ($this->ion_auth->is_admin()) {
				$account['type'] = "Admin";
			}
			else if ($this->ion_auth->is_group('school')) {
				$account['type'] = "School";
			}
			else if ($this->ion_auth->is_group('hotel')) {
				$account['type'] = "Hotel";
			} 
		}
		else {
			$account = null;
		}
		
		$this->template->set('account',$account);
	}
	
	
	
	
	/**
	 * Create the menu links
	 */
	private function _createMenuNav() {
			
		$this->menuItems =
		array (
			'Main/index'		=>	$this->lang->line('public.menu.main'),
			'Main/link2'		=>	$this->lang->line('public.menu.link2'),
			'Main/link3'		=>	$this->lang->line('public.menu.link3'),
			'Main/link4'		=>	$this->lang->line('public.menu.link4'),
			//'Main/linkn'		=>	$this->lang->line('public.menu.linkn'),
		);
	
		$this->load->library('fuelmenu');
	}
	
	
	
	
	
	private function _getMenus() {
		
		$menus = array();
		
		// render the mainMenu
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading'); // set the current link as active
		$menus['main'] = $this->fuelmenu->render($this->menuItems,$active,NULL,'basic');
		
		// render the footerMenu
		$active = $this->uri->rsegment(1).$this->uri->slash_rsegment(2,'leading'); // set the current link as active
		$menus['footer'] = $this->fuelmenu->render($this->menuItems,$active,NULL,'delimited');
		
		// render the loginMenu
		if ($this->ion_auth->logged_in()) {
			$menus['login'] = $this->fuelmenu->render(array('Login/index'=>$this->ion_auth->get_user()->email),null,null,'basic');
		}
		else {
			$menus['login'] = $this->fuelmenu->render(array('Login/index'=>'login'),null,null,'basic');
		}
		
		// render the languageMenu
		$language = array (
			$this->lang->switch_uri('en')=>'en',
			$this->lang->switch_uri('pt')=>'pt',
		);
		$menus['language'] = $this->fuelmenu->render($language,null,null,'basic');
		
		return $menus;
	}
	
	
	
	
	/**
	 * Entry point
	 */
	public function index($sublink='') {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		$this->template->set('groupLibraries',array('jquery'));
		
		// set Menu entries
		$this->template->set('menus',$this->_getMenus());
		
		if (empty($sublink))
			$this->template->build($this->views['index'],$this->Public_model->getLayoutInfo());
		else {	 
			$this->template->build($this->views['index'].$sublink,$this->Public_model->getLayoutInfo());
		}
	}
	
	
	public function link2() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		$this->template->set('groupLibraries',array('jquery'));
		
		// set Menu entries
		$this->template->set('menus',$this->_getMenus());
		
		// call view
		$this->template->build($this->views['link2'],$this->Public_model->getLayoutInfo());
	}
	
	
	public function link3() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		$this->template->set('groupLibraries',array('jquery'));
		
		// set Menu entries
		$this->template->set('menus',$this->_getMenus());
		
		// call view
		$this->template->build($this->views['link3'],$this->Public_model->getLayoutInfo());
	}
	
	
	public function link4() {
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		$this->template->set('groupLibraries',array('jquery'));
		
		// set Menu entries
		$this->template->set('menus',$this->_getMenus());
		
		// call view
		$this->template->build($this->views['link4'],$this->Public_model->getLayoutInfo());	
	}
	
	public function linkn() {
		
		// get layout info
		$data = $this->Public_model->getLayoutInfo();
		
		// witch libraries we need in this view? only those will be loaded in the head view file
		$data['libraries'] = array();
		
		// load local assets
		$this->carabiner->css('public.css');
		
		// render the menu
		$data = array_merge($data,$this->_getMenus());
		
		// call and send data to view
		$this->load->view('public/public_lnkn',$data);
	}
	
	
	
	
	
	
	
	public function login() {
		
		// only enter here if there is a login request
		if($this->input->post('login')) {
			
			// set form rules
			$this->form_validation->set_rules('username','user',		'required');
			$this->form_validation->set_rules('password','password', 	'required');
				
			// define error delimiters so these can be customized with css styles
			$this->form_validation->set_error_delimiters('<p class="error">','</p>');
			
			// see if the previous rules are ok ...
			if ($this->form_validation->run()) {
				if ($_POST) {
					$user = $this->input->post('username', true);
					$pass = $this->input->post('password', true);
						
					// log in ...
					if ($this->ion_auth->login($user,$pass,false)) {
						/*if ($this->ion_auth->is_admin())
							redirect("Admin/index");
						else if($this->ion_auth->is_group("hotel")) {
							$this->session->set_userdata('item','value');
							redirect("Hotel/index");
						}
						else if($this->ion_auth->is_group("school"))
							redirect("School/index");
						else {
							// it should never reach this point ... but if he do  show login form again
							redirect("Main/Login");
						}*/
						// ... go to the current page
						redirect('Main/index');
					}
					else {	// ... login attempt failed
						redirect("Main/Login");
					}
				}
			}
			else {	// ... the rules dont apply? show login form again ...
				redirect("Main/Login");
			}
		} 
			
		// witch libraries we need in this view? only those will be loaded in the head view file
		$this->template->set('groupLibraries',array('jquery'));
		
		// set Menu entries
		$this->template->set('menus',$this->_getMenus());
		
		// call view
		$this->template->build($this->views['login'],$this->Public_model->getLayoutInfo());
	}
	
	
	public function logout() {
		$this->ion_auth->logout();
		redirect('Main/index');
	}
	
	
	
	/**
	* check if someone is loged in, if so ... redirect them accordingly
	*/
	private function check_logon_users() {
	
		if ($this->ion_auth->is_admin()) {
			redirect('Admin/index');
		}
		else {
			if ($this->ion_auth->is_group($this->SchGroup)) {
				redirect('School/index');
			}
			else if($this->ion_auth->is_group($this->HotGroup)) {
				redirect('Hotel/index');
			}
		}
	}
	
	
	
	public function tmp() {
		//$this->Public_model->tmp();
		//$this->firephp->log($this->db);
	}
	
	
	
	
	
	
	
}